/**
 * @version		$id$
 * @package 	K2mart
 * @author 		JoomlaWorks http://www.joomlaworks.gr
 * @copyright Copyright (c) 2006 - 2010 JoomlaWorks, a business unit of Nuevvo Webware Ltd. All rights reserved.
 * @license 	Commercial - This code cannot be redistributed without permission from Nuevvo Webware Ltd.
 */

var tax_rates = new Array();

function doRound(x, places) {
	return Math.round(x * Math.pow(10, places)) / Math.pow(10, places);
}

function getTaxRate() {
	var selected_value = document.adminForm.product_tax_id.selectedIndex;
	var parameterVal = document.adminForm.product_tax_id[selected_value].value;

	if ( (parameterVal > 0) && (tax_rates[parameterVal] > 0) ) {
		return tax_rates[parameterVal];
	} else {
		return 0;
	}
}

function updateGross() {
	if( document.adminForm.product_price.value != '' ) {
		var taxRate = getTaxRate();

		var r = new RegExp("\,", "i");
		document.adminForm.product_price.value = document.adminForm.product_price.value.replace( r, "." );

		var grossValue = document.adminForm.product_price.value;

		if (taxRate > 0) {
			grossValue = grossValue * (taxRate + 1);
		}

		document.adminForm.product_price_incl_tax.value = doRound(grossValue, 5);
	}
}

function updateNet() {
	if( document.adminForm.product_price_incl_tax.value != '' ) {
		var taxRate = getTaxRate();

		var r = new RegExp("\,", "i");
		document.adminForm.product_price_incl_tax.value = document.adminForm.product_price_incl_tax.value.replace( r, "." );

		var netValue = document.adminForm.product_price_incl_tax.value;

		if (taxRate > 0) {
			netValue = netValue / (taxRate + 1);
		}

		document.adminForm.product_price.value = doRound(netValue, 5);
	}
}

function updateDiscountedPrice() {
	if( document.adminForm.product_price.value != '' ) {
		try {
			var selected_discount = document.adminForm.product_discount_id.selectedIndex;
			var discountCalc = document.adminForm.product_discount_id[selected_discount].id;
						var origPrice = document.adminForm.product_price_incl_tax.value;
			
			if( discountCalc ) {
				eval( 'var discPrice = ' + origPrice + discountCalc );
				if( discPrice != origPrice ) {
					document.adminForm.discounted_price_override.value = discPrice.toFixed( 2 );
				} else {
					document.adminForm.discounted_price_override.value = '';
				}
			}
		}
		catch( e ) { }
	}
}


window.addEvent('domready', function(){
	updateGross();
	updateDiscountedPrice();
	
	if($('k2martAvailabilityImg').getProperty('value')!=''){
		new Element('img',{'src':k2martLiveSite+'components/com_virtuemart/themes/default/images/availability/'+$('k2martAvailabilityImg').getProperty('value')}).injectInside($('k2martAvailabilityImgPreview'));
	}
	
	$('k2martAvailabilityImg').addEvent('change', function(){
		if (this.getProperty('value') != '') {
			$('k2martAvailabilityImgPreview').empty();
			new Element('img',{'src':k2martLiveSite+'components/com_virtuemart/themes/default/images/availability/'+this.getProperty('value')}).injectInside($('k2martAvailabilityImgPreview'));
			$$('input[name=product_availability]').setProperty('value', this.getProperty('value'));
		} else {
			$('k2martAvailabilityImgPreview').empty();
			$$('input[name=product_availability]').setProperty('value', '');
		}
	});
	
});


